// 导入db模块
const db = require('../config/db');

// 导航类
class Classify {
    // 添加导航（分类）
    add(classify_text,classify_src='') {
        return new Promise((resolve, reject) => {
            // `选择 字段 来自 导航数据表  如果字段等于传递过来的值 就返回符合条件执行结果`
            let sql = `select classify_text from db_classify where classify_text='${classify_text}'`
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            // 表示没有这个导航
                            let sql2 = `insert into db_classify(classify_text,classify_src) values('${classify_text}','${classify_src}')`;
                            db.query(sql2).then(
                                () => {
                                    resolve({
                                        code: 200,
                                        msg: `"${classify_text}"这个导航添加成功。`
                                    })
                                },
                                () => {
                                    reject({
                                        code: -1,
                                        msg: `添加导航失败。`
                                    })
                                }
                            )

                        } else {
                            // 表示已经存在这个导航
                            resolve({
                                code: 1,
                                msg: `"${classify_text}"这个导航已经存在。`
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询导航失败。'
                        })
                    }
                )
        })
    }

    // 获取导航列表
    list() {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_classify`;
            db.query(sql).then(
                (rows) => {
                    resolve({
                        code: 200,
                        msg: '获取导航列表成功。',
                        result: rows
                    })
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: '获取导航列表失败。',
                        err
                    })
                }
            )
        })
    }

    // 删除导航
    remove(id) {
        return new Promise((resolve, reject) => {
            let sql = `select classify_text from db_classify where id = '${id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该导航不存在。'
                            })
                        }
                        else {
                            // 删除导航
                            let sql2 = `delete from db_classify where id = '${id}'`;
                            db.query(sql2)
                                .then(
                                    () => {
                                        resolve({
                                            code: 200,
                                            msg: '删除导航成功。',
                                        })
                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '删除导航失败。'
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询导航失败。',
                            err
                        })
                    }
                )
        })
    }

    // 设置热门导航
    hot(classify_hot, id) {
        // classify_hot: 1 表示热门导航
        return new Promise((resolve, reject) => {
            let sql = `update db_classify set classify_hot= '${classify_hot}' where  id= '${id}'`;
            db.query(sql)
                .then(
                    () => {
                        resolve({
                            code: 200,
                            msg: '修改热门导航成功。'
                        })
                    },
                    () => {
                        resolve({
                            code: 200,
                            msg: '修改热门导航失败。'
                        })
                    }
                )
        })
    }

    // 添加子类
    add_child(id, classify_text, classify_title, classify_id) {
      return new Promise((resolve, reject)=>{
          // `选择 字段 来自 导航数据表  如果字段等于传递过来的值 就返回符合条件执行结果`
          let sql = `select classify_title from db_classify_child where classify_title='${classify_title}'`
          db.query(sql)
              .then(
                  (rows) => {
                      if (rows.length == 0) {
                          // 表示没有这个导航
                          let sql2 = `insert into db_classify_child(id,classify_text,classify_title,classify_id) 
                          values('${id}','${classify_text}','${classify_title}','${classify_id}')`;
                          db.query(sql2).then(
                              () => {
                                  resolve({
                                      code: 200,
                                      msg: `"${classify_text}"这个导航子分类添加成功。`
                                  })
                              },
                              () => {
                                  reject({
                                      code: -1,
                                      msg: `添加导航子分类失败。`
                                  })
                              }
                          )
  
                      } else {
                          // 表示已经存在这个导航
                          resolve({
                              code: 1,
                              msg: `"${classify_text}"这个导航子分类已经存在。`
                          })
                      }
                  },
                  (err) => {
                      reject({
                          code: -1,
                          msg: '查询导航子分类失败。'
                      })
                  }
              )
      })
    }

    // 获取导航子分类列表
    list_child(classify_text) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_classify_child where classify_text = '${classify_text}'`;
            db.query(sql).then(
                (rows) => {
                    resolve({
                        code: 200,
                        msg: '获取导航子分类列表成功。',
                        result: rows
                    })
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: '获取导航子分类列表失败。',
                        err
                    })
                }
            )
        })
    }

    // 删除子分类
    remove_child(classify_id) {
        return new Promise((resolve, reject) => {
            let sql = `select classify_title from db_classify_child where classify_id = '${classify_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该导航不存在。'
                            })
                        }
                        else {
                            // 删除导航
                            let sql2 = `delete from db_classify_child where classify_id = '${classify_id}'`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        if(result.affectedRows === 1){
                                            resolve({
                                                code: 200,
                                                msg: '删除导航子分类成功。',
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '删除导航子分类失败。',
                                            })
                                        }
                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '删除导航子分类失败。'
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询导航失败。',
                            err
                        })
                    }
                )
        })
    }

    // 删除所有子分类
    remove_child_all(id) {
        return new Promise((resolve, reject) => {
            let sql = `select classify_title from db_classify_child where id = '${id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该导航不存在。'
                            })
                        }
                        else {
                            // 删除导航
                            let sql2 = `delete from db_classify_child where id = '${id}'`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        resolve({
                                            code: 200,
                                            msg: '删除导航所有子分类成功。',
                                        })
                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '删除导航子分类失败。'
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询导航失败。',
                            err
                        })
                    }
                )
        })
    }


    // 设置分类导航图标
    set_icon(classify_src, id) {
        // classify_hot: 1 表示热门导航
        return new Promise((resolve, reject) => {
            let sql = `update db_classify set classify_src= '${classify_src}' where  id= '${id}'`;
            db.query(sql)
                .then(
                    () => {
                        resolve({
                            code: 200,
                            msg: '设置导航图标成功。'
                        })
                    },
                    () => {
                        resolve({
                            code: 200,
                            msg: '设置导航图标失败。'
                        })
                    }
                )
        })
    }

    // 统计总数量
    total(){
        return new Promise((resolve,reject)=>{
            let sql = 'select count(*) as total from db_classify';
            db.query(sql)
            .then(
                result => {
                    resolve({
                        code: 200,
                        msg: "统计数据成功。",
                        result: result[0]
                    })
                },
                err => {
                    reject({
                        code: -1,
                        msg: "统计信息失败",
                        err
                    })
                }
            )
        })
    }
}

module.exports = new Classify();